package com.sc.nft.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.sc.nft.dao.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.CollectionIncrementalLossCalculationDTO;
import com.sc.nft.entity.dto.IncrementalLossCalculationDTO;
import com.sc.nft.entity.vo.GetGiveMetaWalleFeeVO;
import com.sc.nft.entity.vo.IncrementalLossCalculationVO;
import com.sc.nft.entity.vo.metaWalletDTO.MetaWalletCrossUserVO;
import com.sc.nft.entity.vo.metaWalletDTO.UserGetGiveMetaWalleFeeVO;
import com.sc.nft.enums.CoinCategoryEnum;
import com.sc.nft.enums.CollectionOriginalTypeEnum;
import com.sc.nft.enums.MarketTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 藏品赠送(CollectionGift)表服务实现类
 *
 * @author 王伟成
 * @since 2024-03-16 15:19:26
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class CollectionGiftServiceImpl implements CollectionGiftService {

    private final CollectionGiftDao collectionGiftDao;
    private final UserWalletService userWalletService;
    private final UserAssetsDao userAssetsDao;
    private final UserEcologyPointsService userEcologyPointsService;
    private final MetaWalletService metaWalletService;
    private final DigitalCollectionDao digitalCollectionDao;
    private final DigitalCollectionProductInfoDao digitalCollectionProductInfoDao;
    private final CollecionLinkContrastRecordsDao collecionLinkContrastRecordsDao;
    private final UserCollectionDao userCollectionDao;
    private final UserRelevanceService userRelevanceService;
    private final SecondCollectionConfigDao secondCollectionConfigDao;


    @Override
    public List<IncrementalLossCalculationVO> incrementalLossCalculation(CollectionIncrementalLossCalculationDTO incrementalLossCalculationDTO, UserInfo user) {
        List<IncrementalLossCalculationVO>list=new ArrayList<>();
        List<CollectionGift> collectionId = collectionGiftDao.getCollectionId(incrementalLossCalculationDTO.getCollectionId());
        if (CollUtil.isNotEmpty(collectionId)) {
            collectionId.forEach(propGift -> {
                // 根据propId,获取所需要的条件.根据不同的条件做不同的返回计算
                IncrementalLossCalculationVO incrementalLossCalculationVO=new IncrementalLossCalculationVO();
                // 获取用户的数据
                Integer consumeType = propGift.getConsumeType();
                if (consumeType.equals(1)) {
                    UserAssets byUserIdAndCoinId = userAssetsDao.getByUserIdAndCoinId(user.getId(), CoinCategoryEnum.INTEGRAL.getCode());
                    incrementalLossCalculationVO.userDebris(byUserIdAndCoinId);
                }else if (consumeType.equals(2)){
                    UserWallet byUserId = userWalletService.getByUserId(user.getId());
                    incrementalLossCalculationVO.userBalance(byUserId);
                }else if (consumeType.equals(3)){
                    UserEcologyPoints userEcologyPoints = userEcologyPointsService.getOneByUserId(user.getId());
                    incrementalLossCalculationVO.userEcology(userEcologyPoints);
                }
                incrementalLossCalculationVO.calculate(propGift,incrementalLossCalculationDTO.getCollectionNum());
                list.add(incrementalLossCalculationVO);
            });
        }
        return list;
    }

    @Override
    public UserGetGiveMetaWalleFeeVO getGiveMetaWalleFee(UserInfo user, String userAddr, Long collectionId, List<Long> userCollectionIds) throws IllegalAccessException {
        UserGetGiveMetaWalleFeeVO userGetGiveMetaWalleFeeVO = new UserGetGiveMetaWalleFeeVO();
        MetaWalletCrossUserVO metaWalletCrossUserVO = metaWalletService.checkUeerArrd(userAddr);
        userGetGiveMetaWalleFeeVO.setAvatar(metaWalletCrossUserVO.getUrl());
        userGetGiveMetaWalleFeeVO.setNickeName(metaWalletCrossUserVO.getNickname());


        DigitalCollection collection = digitalCollectionDao.getById(collectionId);
        Assert.notNull(collection, () -> new GlobalRunTimeException("藏品不存在"));

        Assert.isTrue(collection.getIsGiveImeta(), () -> new GlobalRunTimeException("此藏品暂未开启跨链转增"));

        CollecionLinkContrastRecords contrastRecords = collecionLinkContrastRecordsDao.getOneByCollectionId(collectionId);
        Assert.notNull(contrastRecords, () -> new GlobalRunTimeException("此藏品暂未开启跨链转增"));


        List<SecondCollectionConfig> brandIncubationList = secondCollectionConfigDao.getListByStatusAndMarketType(MarketTypeEnum.BRAND_INCUBATION);
        List<Long> secondCollectionIds = brandIncubationList.stream().map(SecondCollectionConfig::getId).collect(Collectors.toList());

        List<UserCollection> userCollection = userCollectionDao.getGiveListByCollectionIdAndUserCollectionIds(user.getId(), collectionId, userCollectionIds,secondCollectionIds);
        Assert.isTrue(userCollection.size() == userCollectionIds.size(), () -> new GlobalRunTimeException("存在不符合转增的藏品"));

        //编号集合
        List<Integer> collect = userCollection.stream().map(UserCollection::getNumbering).collect(Collectors.toList());
        Assert.isTrue(metaWalletService.checkCollectionNumerber(user,contrastRecords.getCollectionId(),collect,userAddr),()->new GlobalRunTimeException("存在不符合转增的藏品"));



        List<GetGiveMetaWalleFeeVO>list=new ArrayList<>();
        List<CollectionGift> collectionGifts = collectionGiftDao.getConfListByCollectionIdAndType(collectionId, CollectionOriginalTypeEnum.MATE_WALLET);
        if (CollUtil.isNotEmpty(collectionGifts)) {
            collectionGifts.forEach(propGift -> {
                // 根据propId,获取所需要的条件.根据不同的条件做不同的返回计算
                GetGiveMetaWalleFeeVO getGiveMetaWalleFeeVO=new GetGiveMetaWalleFeeVO();
                // 获取用户的数据
                Integer consumeType = propGift.getConsumeType();
                if (consumeType.equals(1)) {
                    UserAssets byUserIdAndCoinId = userAssetsDao.getByUserIdAndCoinId(user.getId(), CoinCategoryEnum.INTEGRAL.getCode());
                    getGiveMetaWalleFeeVO.userDebris(byUserIdAndCoinId);
                }else if (consumeType.equals(2)){
                    UserWallet byUserId = userWalletService.getByUserId(user.getId());
                    getGiveMetaWalleFeeVO.userBalance(byUserId);
                }else if (consumeType.equals(3)){
                    UserEcologyPoints userEcologyPoints = userEcologyPointsService.getOneByUserId(user.getId());
                    getGiveMetaWalleFeeVO.userEcology(userEcologyPoints);
                }
                getGiveMetaWalleFeeVO.calculate(propGift,userCollection.size());
                list.add(getGiveMetaWalleFeeVO);
            });
        }
        userGetGiveMetaWalleFeeVO.setList(list);
        return userGetGiveMetaWalleFeeVO;
    }

}
